﻿#region License
// (c) Intergen.
// This source is subject to the Microsoft Public License (Ms-PL).
// Please see http://go.microsoft.com/fwlink/?LinkID=131993 for details.
// All other rights reserved.
#endregion

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using TextGlow.Control.Utilities;

namespace TextGlow.Control.Model
{
  public class CellMargins
  {
    private Unit? _top;
    private Unit? _left;
    private Unit? _bottom;
    private Unit? _right;
    private Func<Func<CellMargins, object>, object> _valueGetter;

    public Func<Func<CellMargins, object>, object> ValueGetter
    {
      // if no value getter has been defined, get the value off the current object
      get { return _valueGetter ?? (g => g(this)); }
    }

    public Unit? Right
    {
      get { return (Unit?)_valueGetter(m => m._right); }
      set { _right = value; }
    }

    public Unit? Bottom
    {
      get { return (Unit?)_valueGetter(m => m._bottom); }
      set { _bottom = value; }
    }

    public Unit? Left
    {
      get { return (Unit?)_valueGetter(m => m._left); }
      set { _left = value; }
    }

    public Unit? Top
    {
      get { return (Unit?)_valueGetter(m => m._top); }
      set { _top = value; }
    }

    public CellMargins(XElement element, Func<Func<CellMargins, object>, object> valueGetter)
    {
      _valueGetter = valueGetter;

      if (element == null)
        return;

      XNamespace ns = element.Name.Namespace;

      _left = ModelUtils.ToUnit(XmlUtils.GetValue<int>(element.Element(ns + "left"), "w"),
        XmlUtils.GetValue(element.Element(ns + "left"), "type"));
      _right = ModelUtils.ToUnit(XmlUtils.GetValue<int>(element.Element(ns + "right"), "w"),
        XmlUtils.GetValue(element.Element(ns + "right"), "type"));
      _top = ModelUtils.ToUnit(XmlUtils.GetValue<int>(element.Element(ns + "top"), "w"),
        XmlUtils.GetValue(element.Element(ns + "top"), "type"));
      _bottom = ModelUtils.ToUnit(XmlUtils.GetValue<int>(element.Element(ns + "bottom"), "w"), 
        XmlUtils.GetValue(element.Element(ns + "bottom"), "type"));
    }
  }
}